/**
 * 
 * Copyright (C) 2011 Cody Stoutenburg . All rights reserved.
 *
 *       This program is free software; you can redistribute it and/or
 *       modify it under the terms of the GNU Lesser General Public License
 *       as published by the Free Software Foundation; either version 2.1
 *       of the License, or (at your option) any later version.
 *
 *       This program is distributed in the hope that it will be useful,
 *       but WITHOUT ANY WARRANTY; without even the implied warranty of
 *       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *       GNU Lesser General Public License for more details.
 *
 *       You should have received a copy of the GNU Lesser General Public License
 *       along with this program; if not, write to the Free Software
 *       Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. 
 * 
 */
package net.alteiar.plateform.game.view.sprite;

import java.awt.Graphics;
import java.awt.Image;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Cody Stoutenburg
 * 
 */
public class AnimatedSprite implements Sprite {
	/** The images to be drawn for this sprites */
	private final List<Image> images;

	private Double currentDistance;
	// private final Integer distance;
	private Integer currentImage;

	/**
	 * Create a new sprites based on an images
	 * 
	 * @param images
	 *            The images that is this sprites
	 */
	public AnimatedSprite(Image img) {
		this.images = new ArrayList<Image>();
		currentDistance = 0.0;
		currentImage = 0;
		addImage(img);
	}

	public void addImage(Image img) {
		images.add(img);
	}

	/**
	 * Get the width of the drawn sprites
	 * 
	 * @return The width in pixels of this sprites
	 */
	@Override
	public int getWidth() {
		return images.get(0).getWidth(null);
	}

	/**
	 * Get the height of the drawn sprites
	 * 
	 * @return The height in pixels of this sprites
	 */
	@Override
	public int getHeight() {
		return images.get(0).getHeight(null);
	}

	/**
	 * Draw the sprites onto the graphics context provided
	 * 
	 * @param g
	 *            The graphics context on which to draw the sprites
	 * @param x
	 *            The x location at which to draw the sprites
	 * @param y
	 *            The y location at which to draw the sprites
	 */
	@Override
	public void draw(Graphics g, int x, int y, long frameInMs, Double speedRatio) {
		g.drawImage(images.get(currentImage), x, y, null);

		currentDistance++;
		if (currentDistance >= (1 - speedRatio) * (2000.0 / frameInMs)) {
			currentImage = (currentImage + 1) % images.size();
			currentDistance = 0.0;
		}
	}
}
